package com.wj.hourupdate.datasource;

import com.wj.hourupdate.util.Logger;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
	private static final String DB_NAME = "hourupdate.db";
	private static final int DB_VERSION = 1;
	
	// table settings
	public static final String TB_SETTING = "setting";
	public static final String TB_SETTING_COL_IP = "ip";
	public static final String TB_SETTING_COL_PORT = "port";
	
	// table news
	public static final String TB_NEWS = "news";
	public static final String TB_NEWS_COL_CNID = "cnid";
	public static final String TB_NEWS_COL_CNLINK = "cnlink";
	public static final String TB_NEWS_COL_CNLOGO = "cnlogo";
	public static final String TB_NEWS_COL_CDNID = "cdnid";
	public static final String TB_NEWS_COL_CDNTITLE = "cdntitle";
	public static final String TB_NEWS_COL_CDNDESC = "cdndesc";
	public static final String TB_NEWS_COL_CDNLINK = "cdnlink";
	public static final String TB_NEWS_COL_CDNPUBDATE = "cdnpubdate";
	public static final String TB_NEWS_COL_CDNIMAGE = "cdnimage";
	public static final String TB_NEWS_COL_BANNER = "banner";
	
	private final String SQL_CREATE_TB_SETTING = 
			"CREATE TABLE " + TB_SETTING + 
			" ( " + TB_SETTING_COL_IP + " TEXT, " +
			TB_SETTING_COL_PORT + " TEXT );";
	
	private final String SQL_CREATE_TB_NEWS = 
			"CREATE TABLE " + TB_NEWS + 
			" ( " + TB_NEWS_COL_CDNID + " TEXT PRIMARY KEY, " +
			TB_NEWS_COL_CNID + " TEXT, " +
			TB_NEWS_COL_CNLINK + " TEXT, " +
			TB_NEWS_COL_CNLOGO + " TEXT, " + 
			TB_NEWS_COL_CDNTITLE + " TEXT, " +
			TB_NEWS_COL_CDNDESC + " TEXT, " + 
			TB_NEWS_COL_CDNLINK + " TEXT, " +
			TB_NEWS_COL_CDNPUBDATE + " TEXT, " +
			TB_NEWS_COL_CDNIMAGE + " TEXT, " +
			TB_NEWS_COL_BANNER + " TEXT " +
			" ); ";
	
	public DatabaseHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SQL_CREATE_TB_NEWS);
		db.execSQL(SQL_CREATE_TB_SETTING);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TB_NEWS);
		db.execSQL("DROP TABLE IF EXISTS " + TB_SETTING);
		onCreate(db);
	}

}
